package org.jeecg.modules.rental.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.rental.entity.RentalEBikeType;
import org.jeecg.modules.rental.service.IRentalEBikeTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 车辆类型
 * @Author: sean
 * @Date: 2024-04-26
 * @Version:V2.0
 */
@Slf4j
@Api(tags = "车辆类型")
@RestController
@RequestMapping("/rental/ebike-type")
public class RentalEBikeTypeController extends JeecgController<RentalEBikeType, IRentalEBikeTypeService> {

    @Autowired
    private IRentalEBikeTypeService rentalCarTypeService;

    /**
     * 分页列表查询
     * @param rentalCarType
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "车辆类型-分页列表查询")
    @ApiOperation(value = "车辆类型-分页列表查询", notes = "车辆类型-分页列表查询")
    @GetMapping(value = "/list")
    @PermissionData(pageComponent="rental/ebike/typelist")
    public Result<IPage<RentalEBikeType>> queryPageList(RentalEBikeType rentalCarType,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req)
    {
        QueryWrapper<RentalEBikeType> queryWrapper = QueryGenerator.initQueryWrapper(rentalCarType, req.getParameterMap());
        Page<RentalEBikeType> page = new Page<>(pageNo, pageSize);
        IPage<RentalEBikeType> pageList = rentalCarTypeService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     * @param rentalCarType
     * @return
     */
    @AutoLog(value = "车辆类型-添加")
    @ApiOperation(value = "车辆类型-添加", notes = "车辆类型-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody RentalEBikeType rentalCarType) {
        rentalCarTypeService.add(rentalCarType);
        return Result.OK("common.add.success");
    }

    /**
     * 编辑
     * @param rentalCarType
     * @return
     */
    @AutoLog(value = "车辆类型-编辑")
    @ApiOperation(value = "车辆类型-编辑", notes = "车辆类型-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody RentalEBikeType rentalCarType) {
        rentalCarTypeService.updateById(rentalCarType);
        return Result.OK("common.edit.success");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @AutoLog(value = "车辆类型-通过id删除")
    @ApiOperation(value = "车辆类型-通过id删除", notes = "车辆类型-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        rentalCarTypeService.removeById(id);
        return Result.OK("common.delete.success");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @AutoLog(value = "车辆类型-批量删除")
    @ApiOperation(value = "车辆类型-批量删除", notes = "车辆类型-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.rentalCarTypeService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("common.delete.batch.success");
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    //@AutoLog(value = "车辆类型-通过id查询")
    @ApiOperation(value = "车辆类型-通过id查询", notes = "车辆类型-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<RentalEBikeType> queryById(@RequestParam(name = "id", required = true) String id) {
        RentalEBikeType rentalCarType = rentalCarTypeService.getById(id);
        if (rentalCarType == null) {
            return Result.error("common.query.not.found", null);
        }
        return Result.OK(rentalCarType);
    }

    /**
     * 导出excel
     * @param request
     * @param rentalCarType
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RentalEBikeType rentalCarType) {
        return super.exportXls(request, rentalCarType, RentalEBikeType.class, "车辆类型");
    }

    /**
     * 通过excel导入数据
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RentalEBikeType.class);
    }


    /**
     * 车辆类别列表
     * @return
     */
    @ApiOperation(value = "车辆类别列表", notes = "车辆类别列表")
    @GetMapping(value = "/type-list")
    public Result<List<RentalEBikeType>> list() {
        return Result.OK(rentalCarTypeService.list());
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    //@AutoLog(value = "车辆类型-通过id查询")
    @ApiOperation(value = "车辆类型-通过id查询", notes = "车辆类型-通过id查询")
    @GetMapping(value = "/typeById")
    public Result<RentalEBikeType> typeById(@RequestParam(name = "id", required = true) String id) {
        RentalEBikeType rentalCarType = rentalCarTypeService.getById(id);
        return Result.OK(rentalCarType);
    }


}
